//
//  RegisterGetCardViewController.m
//  GYMWinners
//
//  Created by Logic Solutions on 4/5/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RegisterGetCardViewController.h"
#import "SignViewController.h"
#import "SpaceViewController.h"
#import "NearbyViewController.h"
#import "TopboardViewController.h"


#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h"
#import "MBProgressHUD.h"
#import "BaseroutAPI.h"
#import "AppDelegate.h"


@interface RegisterGetCardViewController ()

@end

@implementation RegisterGetCardViewController
@synthesize getCardSegment;
@synthesize CardDesc;
@synthesize tabBarController;
@synthesize spaceNavController;
@synthesize signNavController;
@synthesize nearbyNavController;
@synthesize topNavController;

-(IBAction)finishRegister{
    
    // Summit
    NSMutableDictionary *slist = [[NSMutableDictionary alloc] init];
    [slist setObject:[[BaseroutAPI sharedInstance] zGetVarEx:@"customerid"] forKey:@"cid"];
    [slist setObject:[[BaseroutAPI sharedInstance] zGetVarEx:@"selectedCommitID"] forKey:@"commitId"];
    [slist setObject:[[BaseroutAPI sharedInstance] zGetVarEx:@"selectedStakeID"] forKey:@"stakeId"];
    
    // If select to want the try card, then send it with default card (assume it is id =1)
    if( getCardSegment.selectedSegmentIndex == 0 )
    {
        [slist setObject:@"1" forKey:@"gymCardId"];
    }
    
    // TODO:
    //[slist setObject:@"1" forKey:@"paymentTypeId"];
    //[slist setObject:@"4000000000009" forKey:@"accountNumber"];
    
    [self zSendServer:slist theTitle:@"请稍等" theCommand:@"/register/submit"];

}

-(void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    NSLog(@"Seg.selectedSegmentIndex:%d",Index);
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = newBackButton; 

    NSArray *cardsArr = (NSArray *)[[BaseroutAPI sharedInstance] zGetObj:@"cardsArr"];
    NSDictionary *card = [cardsArr objectAtIndex:0];
    CardDesc.text = [NSString stringWithFormat:@"%@", [card objectForKey:@"description"]];

    
    NSArray* getCardArr=[[NSArray alloc] initWithObjects:@"获取",@"不获取",nil];
    getCardSegment=[[UISegmentedControl alloc] initWithItems:getCardArr];
    [getCardSegment setFrame:CGRectMake(80, 260, 150, 40)];
    getCardSegment.selectedSegmentIndex=0;
    [getCardSegment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:getCardSegment];
}

- (void)viewDidUnload
{
    [self setCardDesc:nil];
    [super viewDidUnload];
}

-(IBAction)back {
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - ASIFormDataRequest delegate

- (void)requestFinished:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (request.responseStatusCode == 200) {
        
        NSString *responseString = [request responseString];
        NSLog(@"responseString=[%@]", responseString);   
        
        if( [responseString caseInsensitiveCompare:@"success"] == NSOrderedSame)
        {
            // Login OK!
            spaceNavController=[[UINavigationController alloc] init];
            signNavController=[[UINavigationController alloc] init];
            nearbyNavController=[[UINavigationController alloc] init];
            topNavController=[[UINavigationController alloc] init];
            
            
            SpaceViewController* spaceViewController=[[SpaceViewController alloc] init];
            [spaceNavController pushViewController:spaceViewController animated:NO];
            spaceNavController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"空间" image:nil tag:0];
            
            SignViewController* signViewController=[[SignViewController alloc] init];
            [signNavController pushViewController:signViewController animated:NO];
            signNavController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"签到" image:nil tag:0];
            
            
            NearbyViewController* nearbyViewController=[[NearbyViewController alloc] init];
            [nearbyNavController pushViewController:nearbyViewController animated:NO];
            nearbyNavController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"周边" image:nil tag:0];
            
            TopboardViewController* topboardViewController=[[TopboardViewController alloc] init];
            [topNavController pushViewController:topboardViewController animated:NO];
            topNavController.tabBarItem=[[UITabBarItem alloc] initWithTitle:@"排行" image:nil tag:0];
            
            tabBarController=[[UITabBarController alloc] init];
            tabBarController.viewControllers=[NSArray arrayWithObjects:spaceNavController,signNavController,nearbyNavController,topNavController, nil];
            
            AppDelegate *appdel = [[UIApplication sharedApplication] delegate];
            appdel.tabBarController = tabBarController;
            
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.8];
            [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view.window cache:NO];
            
            [self.view.window addSubview:tabBarController.view];
            
            [UIView commitAnimations];
            
            //Add the animation then pop will work next
            //[self.view.window addSubview:tabBarController.view];
            
            [self.navigationController popToRootViewControllerAnimated:NO];
        }
        else
        {
            [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"提交失败"];
        }
        
    } else {
        NSLog(@"Unexpected error");  
        [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
    }
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    NSError *error = [request error];
    NSLog(@"Error=%@", error.localizedDescription);   
    
    [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
}



@end
